package com.example.todotask.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.todotask.R;
import com.example.todotask.database.DatabaseHandler;
import com.example.todotask.database.TaskHandler;
import com.example.todotask.model.Task;
import com.example.todotask.ultils.Constant;
import com.example.todotask.ultils.ToastMessage;

public abstract class BaseActivity extends Activity implements OnClickListener, OnDateSetListener, OnTimeSetListener {

	//
	// Cac thanh phan tren UI
	//
	// ---- EditText----
	protected EditText editTaskName; // input text ten nhiem vu
	protected EditText editTaskNote; // input text noi dung chu y
	// ---- TextView----
	protected TextView lblDueDate; // label hien thi ngay hen
	protected TextView lblDueTime; // label hien thi thoi gian hen
	// ---- ImageView ----
	protected ImageView imgChooseDate; // image chon ngay
	protected ImageView imgChooseTime; // image chon gio
	// ---- DateTime picker -----
	protected DatePickerDialog date_dialog; // hien thi dialog chon ngay
	protected TimePickerDialog time_dialog; // hien thi dialog chon gio
	// ---- Spinner ------
	protected Spinner drpRepeatType;

	//
	// Cac thuoc tinh khac
	//
	protected DatabaseHandler db; // helper database
	protected TaskHandler taskHandler;
	protected Task task;
	protected Intent intent; // intent
	protected Calendar due_date_cal; // ngay gio

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_task);

		// khoi tao db
		db = new DatabaseHandler(this);
		taskHandler = new TaskHandler(db.getTaskDao()); 

		// khoi tao UI
		editTaskName = (EditText) findViewById(R.id.edit_add_task_name);
		editTaskNote = (EditText) findViewById(R.id.edit_add_task_notes);
		lblDueDate = (TextView) findViewById(R.id.text_add_task_due_time);
		lblDueTime = (TextView) findViewById(R.id.text_add_task_space_time);
		imgChooseDate = (ImageView) findViewById(R.id.image_add_task_choose_date);
		imgChooseTime = (ImageView) findViewById(R.id.image_add_task_choose_time);
		drpRepeatType = (Spinner) findViewById(R.id.spinner_add_task_repeat_type);

		// khoi tao calendar
		due_date_cal = GregorianCalendar.getInstance();

		// mac dinh due date va due time la ngay gio hien tai.
		updateDateButtonText();
		updateTimeButtonText();

		// khoi tao repeat type drop
		ArrayAdapter<CharSequence> repeat_adapter = ArrayAdapter
				.createFromResource(this, R.array.array_repeat_type,
						android.R.layout.simple_spinner_item);
		repeat_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		drpRepeatType.setAdapter(repeat_adapter);

		// set default listener
		imgChooseDate.setOnClickListener(this);
		imgChooseTime.setOnClickListener(this);
	}

	// thêm, sửa task.
	protected abstract boolean addTask();
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_add_task_choose_date:
			// chon ngay, show dialog
			date_dialog = new DatePickerDialog(this, this,
					due_date_cal.get(Calendar.YEAR),
					due_date_cal.get(Calendar.MONTH),
					due_date_cal.get(Calendar.DAY_OF_MONTH)) {
				@Override
				public Bundle onSaveInstanceState() {
					if (date_dialog.isShowing()) {
						date_dialog.cancel();
					}
					return super.onSaveInstanceState();
				}
			};
			
			date_dialog.show();
			break;
		case R.id.image_add_task_choose_time:
			// chon gio, show dialog.
			time_dialog = new TimePickerDialog(this, this, 
					due_date_cal.get(Calendar.HOUR_OF_DAY),
					due_date_cal.get(Calendar.MINUTE), 
					true) {
				@Override
				public Bundle onSaveInstanceState() {
					if (time_dialog.isShowing()) {
						time_dialog.cancel();
					}
					return super.onSaveInstanceState();
				}
			};
			
			time_dialog.show();
			break;
		default:
			break;
		}
	}

	/*
	 * Hien thi gio len label man hinh.
	 */
	private void updateTimeButtonText() {
		lblDueTime.setText(DateFormat.format(Constant.TIME_FORMAT, due_date_cal));
	}

	/*
	 * Hien thi ngay len label man hinh.
	 */
	private void updateDateButtonText() {
		lblDueDate.setText(DateFormat.format(Constant.DATE_FORMAT, due_date_cal));
	}
	
	/*
	 * Thiet lap gio khi chon tren dialog Time.
	 */
	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		due_date_cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
		due_date_cal.set(Calendar.MINUTE, minute);
		updateTimeButtonText();
	}

	/*
	 * Thiet lap ngay khi chon tren dialog Date.
	 */
	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		due_date_cal.set(year, monthOfYear, dayOfMonth);
		updateDateButtonText();
	}
	
	/**
	 * Bắt sự kiện item menu click
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_item_save_task:
			boolean result = addTask();
			if (result){
				setResult(RESULT_OK, intent);
				finish();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * Kiểm tra dữ liệu nhập vào.
	 */
	protected boolean isValidInput() {
		String name = editTaskName.getText().toString().trim();
		String note = editTaskNote.getText().toString().trim();
		if (!StringUtils.isNotBlank(name)) {
			// name not null
			ToastMessage.ToastMsg(this, R.string.toast_err_task_name_null);
			return false;
		} 
		
		if (!StringUtils.isNotBlank(note)) {
			// note not null
			ToastMessage.ToastMsg(this, R.string.toast_err_task_note_null);
		}
		
		return true;
	}
}
